package com.fjnu.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fjnu.controller.common.BaseController;
import com.fjnu.entities.EnclosureInstrument;
import com.fjnu.entities.User;
import com.fjnu.service.IEncInstrumentService;
/**
 * 附件表的基本表现层
 * @author 默默
 *
 */
@Controller
public class EncInstrumentController extends BaseController{
	private static Logger logger =LogManager.getLogger(EncInstrumentController.class);
	@Autowired
	private IEncInstrumentService encInstrumentService;
	/**
	 * 根据主件ID查找其下的附件信息
	 * @param mainId
	 * @return
	 */
	@RequestMapping(value="/findByMainId")
	@ResponseBody
	public Map<String,Object> findByMainId(Integer mainId){
		logger.info("查找主件ID为"+mainId+"附件信息");
		if(!init(mainId).get(STATUS).equals(SUCCESS))
			return getData2Result();
		List<EnclosureInstrument> list =encInstrumentService.findByMainId(mainId);
		put("data",list);
		return getData2Result();
	}
	/**
	 * 更新附件信息
	 * @param enclosureInstrument
	 * @return
	 */
	@RequestMapping(value="/updateEnc")
	@ResponseBody
	public Map<String,Object> updateEncIns(EnclosureInstrument enclosureInstrument){
		logger.info("更新附件信息");
		if(!init(enclosureInstrument).get(STATUS).equals(SUCCESS))
			return getData2Result();
		encInstrumentService.updateEncInstrument(enclosureInstrument);
		return getData2Result();
	}
	/**
	 * 添加附件信息
	 * @param enclosureInstrument
	 * @return
	 */
	@RequestMapping(value="/addEnc")
	@ResponseBody
	public Map<String,Object> addEncIns(EnclosureInstrument enclosureInstrument){
		logger.info("添加附件信息");
		if(!init(enclosureInstrument).get(STATUS).equals(SUCCESS))
			return getData2Result();
		enclosureInstrument.setStatusQuo(enclosureInstrument.GOOD);
		Subject currentUser = SecurityUtils.getSubject();  
		Session session = currentUser.getSession();
		User inputPerson=(User) session.getAttribute("subject");
		enclosureInstrument.setInputPerson(inputPerson);
		Date inputTime =new Date();
		enclosureInstrument.setInputTime(inputTime);
		encInstrumentService.addEncInstrument(enclosureInstrument);
		return getData2Result();
	}
	
	@RequestMapping(value="/delEnc")
	@ResponseBody
	public Map<String,Object> delEncIns(int id){
		logger.info("删除单个附件");
		if(!init(id).get(STATUS).equals(SUCCESS))
			return getData2Result();
		encInstrumentService.delEncInstrument(id);
		return getData2Result();
	}
}
